#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from pylenium.driver import Pylenium
import allure
from dragon.locatordetail import LocatorDetail
from dragon.element import DraElement, DraElements
from dragon.should import DraPyleniumShould
from dragon.keyboard import Keyboard

class DraPylenium:
    def __init__(self, py_config):
        self._py = Pylenium(py_config)
        self.env = self._py.config.custom['env']
        self.fake = self._py.fake

    def find(self, ld: LocatorDetail, timeout: int = None):
        '''查找符合locator的第一个元素'''
        if ld.type == 'css':
            element = self._py.get(ld.locator, timeout)
        elif ld.type == 'xpath':
            element = self._py.getx(ld.locator, timeout)
        elif ld.type == 'text':
            element = self._py.contains(ld.locator, timeout)
        else:
            raise ValueError('元素type必须是`css`、`xpath`、`text`其中之一')

        return DraElement(element,ld,self)

    def finds(self, ld: LocatorDetail, timeout: int | None = None):
        '''查找符合locator的所有元素'''
        if ld.type == 'css':
            elements = self._py.find(ld.locator, timeout)
        elif ld.type == 'xpath':
            elements = self._py.findx(ld.locator, timeout)
        else:
            raise Exception('元素查找方法错误')
        return DraElements(elements, ld,self)

    def go(self, direction: str, number: int = 1):
        if direction == 'forward':
            stepText = f'步骤：向前进【{number}】页'
        elif direction == 'back':
            stepText = f'步骤：向后退【{number}】页'
        else:
            raise ValueError(f"参数必须是`forward` 或 `back`，但是传了【{direction}】")
        with allure.step(stepText):
            self.log.info(stepText)
            self._py.go(direction, number)
            return self

    def reload(self):
        stepText = '步骤：刷新页面'
        with allure.step(stepText):
            self.log.info(stepText)
            self._py.reload()
            return self

    def visit(self, url: str):
        stepText = f'步骤：访问url：【{url}】'
        with allure.step(stepText):
            self.log.info(stepText)
            self._py.visit(url)
            return self

    def should(self, timeout: int = 10, ignored_exceptions: list = None):
        should=self._py.should(timeout, ignored_exceptions)
        return DraPyleniumShould(should,self)

    def keyboard(self):
        return Keyboard(self)

    def sleep(self, second: int = 1):
        self._py.wait(use_py=True).sleep(second)
        return self

    def execute_script(self, js: str) -> any:
        return self._py.execute_script(js)

    def execute_async_script(self, js: str) -> any:
        return self._py.execute_async_script(js)

    def screenshot(self, filename: str):
        self._py.screenshot(filename)

    def scroll_to(self, x: int, y: int):
        stepText = f'步骤：滚动到坐标：【{x},{y}】'
        with allure.step(stepText):
            self.log.info(stepText)
            self._py.scroll_to(x, y)
        return self

    @property
    def title(self):
        return self._py.title()

    @property
    def url(self):
        return self._py.url()

    @property
    def log(self):
        return self._py.log

    # self.window_handles = self._py.window_handles
    def window_handles(self):
        return self._py.window_handles

    def wait_for_request(self,apiPath:str):
        return self._py.webdriver.wait_for_request(apiPath)